﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using Model;
using WindowsForms.Class;
using WindowsForms.service;

namespace WindowsForms
{
    public partial class MenuManagement : DevExpress.XtraEditors.XtraForm
    {
        Service1 iQuery;
        List<Menus> lsMenu;
        public MenuManagement()
        {
            iQuery = new Service1();
            InitializeComponent();
            GetMenus();
        }

        private void GetMenus()
        {
            ConvertToList convert = new ConvertToList();
            lsMenu = convert.ConvertTolistMenus(iQuery.SelectMenusAll().Tables[0]);
            gridControlMenu.DataSource = lsMenu;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (MenuProcess frmMenu = new MenuProcess())
            {
                if (frmMenu.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    Menus menuObj = new Menus();
                    menuObj.MenuTypeID = frmMenu.Menu_MenuTypeID;
                    menuObj.MenuName = frmMenu.Menu_Name;
                    menuObj.Price = Convert.ToInt32(frmMenu.Menu_Price);
                    menuObj.Total = Convert.ToInt32(frmMenu.Menu_Total);
                    menuObj.UnitID = frmMenu.Menu_UnitID;
                    menuObj.Picture = frmMenu.Picture;
                    iQuery.InsertMenu(menuObj.MenuTypeID, menuObj.MenuName, menuObj.Price, menuObj.UnitID, menuObj.Total,menuObj.Picture);
                    lsMenu.Add(menuObj);
                   // gvMenu.RefreshData();
                }
            }
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvMenu.GetSelectedRows();
            Menus menuSelected = gvMenu.GetRow(rows[rows.Length - 1]) as Menus;

            using (MenuProcess frmMenu = new MenuProcess(true))
            {
                frmMenu.Menu_Name = menuSelected.MenuName;
                frmMenu.Menu_Price = Convert.ToString(menuSelected.Price);
                frmMenu.Menu_MenuTypeID = menuSelected.MenuTypeID;
                frmMenu.Menu_UnitID = menuSelected.UnitID;
                frmMenu.Menu_Total = Convert.ToString(menuSelected.Total);
                frmMenu.Picture = menuSelected.Picture;
                if (frmMenu.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    menuSelected.MenuName = frmMenu.Menu_Name;
                    menuSelected.Price = Convert.ToInt32(frmMenu.Menu_Price);
                    menuSelected.Total = Convert.ToInt32(frmMenu.Menu_Total);
                    menuSelected.UnitID = frmMenu.Unit_Selected.UnitID;
                    menuSelected.MenuTypeID = frmMenu.MenuType_Selected.MenuTypeID;
                    menuSelected.Picture = frmMenu.Picture;
                    iQuery.UpdateMenu(menuSelected.MenuID, menuSelected.MenuTypeID, menuSelected.MenuName, menuSelected.Price
                        ,menuSelected.UnitID, false, menuSelected.Total,menuSelected.Picture);
                    gvMenu.RefreshData();
                }
            }
        }

        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] rows = gvMenu.GetSelectedRows();
            Menus menuSelected = gvMenu.GetRow(rows[rows.Length - 1]) as Menus;

            string message = string.Format(Constants.MESS_CONFIRMDELETE, menuSelected.MenuName);
            DialogResult dr = MessageBox.Show(message, Constants.MESS_DELETE_CAPTION, MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                iQuery.DeleteMenu(menuSelected.MenuID);
                lsMenu.Remove(menuSelected);
                gvMenu.RefreshData();
            }
        }

        private void gvMenu_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            btnDel.Enabled = gvMenu.SelectedRowsCount > 0;
            btnEdit.Enabled = gvMenu.SelectedRowsCount > 0;
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            GetMenus();
        }

        private void MenuManagement_Load(object sender, EventArgs e)
        {

        }
    }
}